/*
 * Copyright (c) 2020-2022 Jinsp-tech. All rights reserved.
 */

package com.jinsp.networklibrary.command;

import android.util.SparseArray;

/**
 * Created by luoshuai on 1/15/22
 * <p>
 * TCP命令对象享元工厂
 */
public class TCPCommandFactory {

    private final static SparseArray<TCPCommandData> TCP_COMMAND_SPARSE_ARRAY = new SparseArray<>();

    /**
     * 根据命令号获取TCPCommand对象
     *
     * @param commandNumber 命令号
     * @return 命令号对应的TCP命令对象
     */
    public static TCPCommandData getTCPCommand(int commandNumber) {
        TCPCommandData tcpCommandData = TCP_COMMAND_SPARSE_ARRAY.get(commandNumber);
        if (tcpCommandData == null) {
            tcpCommandData = new TCPCommandData(commandNumber, null);
            TCP_COMMAND_SPARSE_ARRAY.put(commandNumber, tcpCommandData);
        }
        return tcpCommandData;
    }

    /**
     * 释放所有命令对象
     */
    public static void release() {
        TCP_COMMAND_SPARSE_ARRAY.clear();
    }
}
